﻿Imports Microsoft.VisualBasic
Imports System.Linq

Namespace MNL.Store

    <Serializable()> _
    Public Class ShoppingCartItem

        Public Property PartID As Integer
        Public Property CatID As Integer
        Public Property Name As String
        Public Property UnitPrice As Decimal
        Public Property Quantity As Integer = 1

        Public Sub New(ByVal thePartID As Integer, ByVal theCatID As Integer, ByVal theName As String, ByVal theUnitPrice As Decimal)
            PartID = thePartID
            CatID = theCatID
            Name = theName
            UnitPrice = theUnitPrice

        End Sub


    End Class

    <Serializable()> _
    Public Class ShoppingCart
        Private _parts As New Dictionary(Of Integer, ShoppingCartItem)
        Public ReadOnly Property Parts As ICollection
            Get
                Return _parts.Values
            End Get

        End Property

        Public ReadOnly Property Total As Decimal
            Get
                Dim sum As Decimal = 0.0

                For Each Part As ShoppingCartItem In _parts.Values
                    sum += Part.UnitPrice * Part.Quantity

                Next
                Return sum
            End Get
        End Property

        Public Sub InsertPart(ByVal id As Integer, ByVal catID As Integer, ByVal name As String, ByVal unitPrice As Decimal)
            If _parts.ContainsKey(id) Then
                _parts(id).Quantity = _parts(id).Quantity + 1
            Else
                _parts.Add(id, New ShoppingCartItem(id, catID, name, unitPrice))
            End If
        End Sub

        'removes a part item from the shopping cart
        Public Sub DeletePartItem(ByVal id As Integer)
            If _parts.ContainsKey(id) Then
                Dim Part As ShoppingCartItem = _parts(id)
                Part.Quantity -= 1
                If Part.Quantity = 0 Then
                    _parts.Remove(id)
                End If
            End If
        End Sub

        'removes all part items of a specified part from the shopping cart
        Public Sub DeletePart(ByVal id As Integer)

            If _parts.ContainsKey(id) Then
                _parts.Remove(id)
            End If
        End Sub

        Public Sub UpdatePartQuantity(ByVal id As Integer, ByVal quantity As Integer)
            If _parts.ContainsKey(id) Then
                Dim part As ShoppingCartItem = _parts(id)
                part.Quantity = quantity
                If part.Quantity <= 0 Then
                    _parts.Remove(id)
                End If
            End If
        End Sub

        'clear the shopping cart
        Public Sub Clear()
            _parts.Clear()
        End Sub
    End Class

    Public Class CurrentUserShoppingCart

        Public Shared Function GetItems() As ICollection
            Return CType(HttpContext.Current.Profile, ProfileCommon).ShoppingCart.Parts

        End Function


        Public Shared Sub DeletePartItem(ByVal id As Integer)
            CType(HttpContext.Current.Profile, ProfileCommon).ShoppingCart.DeletePartItem(id)

        End Sub

        Public Shared Sub DeletePart(ByVal id As Integer)
            CType(HttpContext.Current.Profile, ProfileCommon).ShoppingCart.DeletePart(id)

        End Sub
    End Class

End Namespace